<?php
// ----- >> Extends From Template
class thisPDF extends PDF{
  function Header(){}
  function Footer(){}
}

function terbilang_get_valid($str,$from,$to,$min=1,$max=9){
      $val=false;
      $from=($from<0)?0:$from;
      for ($i=$from;$i<$to;$i++){
        if (((int) $str{$i}>=$min)&&((int) $str{$i}<=$max)) $val=true;
      }
      return $val;
  } 

  function terbilang_get_str($i,$str,$len){
      $numA=array("","satu","dua","tiga","empat","lima","enam","tujuh","delapan","sembilan");
      $numB=array("","se","dua ","tiga ","empat ","lima ","enam ","tujuh ","delapan ","sembilan ");
      $numC=array("","satu ","dua ","tiga ","empat ","lima ","enam ","tujuh ","delapan ","sembilan ");
      $numD=array(0=>"puluh",1=>"belas",2=>"ratus",4=>"ribu", 7=>"juta", 10=>"milyar", 13=>"triliun");
      $buf="";
      $pos=$len-$i;
      switch($pos){
        case 1:
            if (!terbilang_get_valid($str,$i-1,$i,1,1))
              $buf=$numA[(int) $str{$i}];
          break;
        case 2: case 5: case 8: case 11: case 14:
            if ((int) $str{$i}==1){
              if ((int) $str{$i+1}==0)
                $buf=($numB[(int) $str{$i}]).($numD[0]);
              else
                $buf=($numB[(int) $str{$i+1}]).($numD[1]);
            }
            else if ((int) $str{$i}>1){
                $buf=($numB[(int) $str{$i}]).($numD[0]);
            }       
          break;
        case 3: case 6: case 9: case 12: case 15:
            if ((int) $str{$i}>0){
                $buf=($numB[(int) $str{$i}]).($numD[2]);
            }
          break;
        case 4: case 7: case 10: case 13:
            if (terbilang_get_valid($str,$i-2,$i)){
              if (!terbilang_get_valid($str,$i-1,$i,1,1))
                $buf=$numC[(int) $str{$i}].($numD[$pos]);
              else
                $buf=$numD[$pos];
            }           else if((int) $str{$i}>0){
              if ($pos==4)
                $buf=($numB[(int) $str{$i}]).($numD[$pos]);
              else
                $buf=($numC[(int) $str{$i}]).($numD[$pos]);
            }
          break;
      }
      return $buf;
  }
  function toTerbilang($nominal){
      $buf="";
      $str=$nominal."";
      $len=strlen($str);
      
      for ($i=0;$i<$len;$i++){
        $buf=trim($buf)." ".terbilang_get_str($i,$str,$len);
      }
      return trim($buf);
  }

// ----- >> Init
$fpdf = new thisPDF('P','mm','A4');
$fpdf->AddPage();
$fpdf->SetLeftMargin(8);

$fpdf->Ln(0);
$ln_header_y_top  = $fpdf->GetY();

{

// ----- >> Header
$fpdf->Ln(2);

//$Image = APPPATH . 'file/kstore/logo_primkokas.jpg' ; 
$fpdf->Cell( 30, 20, $fpdf->Image(APPPATH . 'file/media/logo_primkokas.jpg' , $fpdf->GetX(), $fpdf->GetY(), 40.68), 0, 0, 'L', false );
//$fpdf->Ln(20);

$fpdf->SetFont('Arial','B',10);
$fpdf->Cell(70,5,"",0,0,'L');
$fpdf->Cell(0,2,"PRIMER KOPERASI KARYAWAN KRAKATAU STEEL",0,0,'L');
$fpdf->Ln(5);
$fpdf->SetFont('Arial','',10);
$fpdf->Cell(87,5,"",0,0,'L');
$fpdf->Cell(0,2,"Gedung Krakatau Junction Lt. 3, Jl. KH. Yasin Neji Komp. PT KS",0,0,'L');
$fpdf->Ln(5);
$fpdf->Cell(151,5,"",0,0,'L');
$fpdf->Cell(0,2,"Cilegon - Banten 42435",0,0,'L');
$fpdf->Ln(5);
$fpdf->Cell(89,5,"",0,0,'L');
$fpdf->Cell(0,2,"Phone +62 (254) 392784, 372289 Fax. + (254) 372290, 398884",0,0,'L');
$fpdf->Ln(5);
$fpdf->Cell(139,5,"",0,0,'L');
$fpdf->Cell(0,2,"Website : www.primkokas.co.id",0,0,'L');
$fpdf->Ln(5);
$fpdf->Cell(144,5,"",0,0,'L');
$fpdf->Cell(0,2,"email: info@primkokas.co.id",0,0,'L');
$fpdf->Ln(5);


$fpdf->Line(9, 42, 200-3, 42); // 197mm from each edge


// ----- >> Data Master

$fpdf->Ln(10);

// ----- >> Data Master
//setting judul laporan dan header tabel

$fpdf->setFont('Arial','B',10);
$fpdf->text(10,40,'Building MGT & MCT Dep.');
//$fpdf->text(10,40,'Laporan Perangkat Pernah Terpakai');
$fpdf->Ln(10);
$yi = 50;
$ya = 50;
$fpdf->text(70,48,'Laporan Data Kontrol Pemakaian Listrik');
$fpdf->setFont('Arial','',8);
$fpdf->setFillColor(222,222,222);
$fpdf->setXY(10,$ya);
$fpdf->Cell(6,6,'NO',1,0,'C',1);
//$fpdf->Cell(33,6,'KODE PERANGKAT',1,0,'C',1);
$fpdf->Cell(35,6,'NANA TENANT',1,0,'C',1);
$fpdf->Cell(30,6,'LOKASI TEMPAT',1,0,'C',1);
$fpdf->Cell(25,6,'NO TOKO',1,0,'C',1);
$fpdf->Cell(20,6,'KWH AWAL',1,0,'C',1);
$fpdf->Cell(20,6,'KWH AKHIR',1,0,'C',1);
$fpdf->Cell(20,6,'PEMAKAIAN',1,0,'C',1);
$fpdf->Cell(30,6,'PERIODE',1,0,'C',1);
//$fpdf->Ln(10);
//$fpdf->Cell(30,6,'KETERANGAN',1,0,'C',1);

//$sql = $query("select *from mhs order by nim");

//Page break setting

//$fpdf = new PDF();
//$fpdf->AddPage();

$i = 10;
$no = 10;
$max = 31;
$row = 6;
$ya = $yi + $row;
$no = 1;
foreach ($data_list as $key => $data_list_output){

    //$fpdf->Ln(10);
    if($ya>=276){
      $ya = 16;
    }

    $fpdf->setXY(10,$ya);
    $fpdf->setFont('arial','',8);
    $fpdf->setFillColor(255,255,255);
    $fpdf->Cell(6,6,$no,1,0,'C',1);
    //$fpdf->Cell(20,6,$data['kode_perangkat'],1,0,'L',1);
    $fpdf->Cell(35,6,$data_list_output['nama_tenant'],1,0,'L',1);
    $fpdf->Cell(30,6,$data_list_output['lokasi_tempat'],1,0,'L',1);
    $fpdf->Cell(25,6,$data_list_output['no_toko'],1,0,'C',1);
    $fpdf->Cell(20,6,$data_list_output['kwh_awal_meter'],1,0,'C',1);
    $fpdf->Cell(20,6,$data_list_output['kwh_akhir_meter'],1,0,'C',1);
    $fpdf->Cell(20,6,$data_list_output['kwh_pemakaian'],1,0,'C',1);
    $fpdf->Cell(30,6,$data_list_output['tanggal'],1,0,'C',1);
    //$fpdf->Cell(30,6,$data['keterangan'],1,0,'C',1);

    $ya = $ya+$row;
  
    //var_dump($ya);
    $no++;
    $i++;
    //$fpdf->AliasNbPages();

  }

//$fpdf->setAutoPageBreak(true);
//$fpdf->addPage();
}
/*$fpdf->Cell(42,5,"Keterangan",0,0,'L');
$fpdf->Cell(2,5,":",0,0,'L');
$fpdf->Cell(20,5,$data['keterangan'],0,0,'L');
$fpdf->Ln(4);
*/

$date = date('d-M-y');

echo $fpdf->Output('Laporan kontrol pemakaian listrik'." "."$date"." ".'.pdf','I');
exit();
?>